﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;

namespace Streaming_Service.Test {
    using Streaming_Service.Crawler;
    using Streaming_Service.Enum;
    using Streaming_Service.Media;
    using Streaming_Service.SQL_Ting;
    using Streaming_Service.User;

    [TestFixture]
    public class TestMisc {
        [Test]
        public void UserEntryLogic() {
            Assert.That(!UserEntry.IsValidBirthday(null));
            Assert.That(!UserEntry.IsValidBirthday(DateTime.Now));
            Assert.That(UserEntry.IsValidBirthday(DateTime.Now.AddYears(-30)));
        }

        [Test]
        public void XBoolLogic() {
            var client = new RentIt11();
            Assert.That(client.IsTrue(XBool.True));
            Assert.That(!client.IsTrue(XBool.False));
            Assert.That(!client.IsTrue(XBool.Ignore));
        }

        [Test]
        public void EnumHelperFunctionality() {
            Assert.That(EnumHelper.IsTrue(XBool.True));
            Assert.That(!EnumHelper.IsTrue(XBool.False));
            Assert.That(!EnumHelper.IsTrue(XBool.Ignore));

            Assert.That(EnumHelper.GetParentalGuidance(null) == ParentalGuidance.G);
            Assert.That(EnumHelper.GetParentalGuidanceExt(null, ParentalGuidance.UR) == ParentalGuidance.UR);
            Assert.That(EnumHelper.GetParentalGuidance("PG13") == ParentalGuidance.PG13);
            Assert.That(EnumHelper.GetParentalGuidance("DUMMY", ParentalGuidance.PG) == ParentalGuidance.PG);
            Assert.That(EnumHelper.GetParentalGuidanceExt("!!Rµ@£$", ParentalGuidance.NC17) == ParentalGuidance.R);

            var client = new RentIt11();
            Assert.That(client.GetParentalGuidance("PG13") == ParentalGuidance.PG13);
        }

        [Test]
        public void DBFunctions() {
            Assert.That(DBFunc.MD5("a") == "0cc175b9c0f1b6a831c399e269772661");
            Assert.That(DBFunc.MD5("") == "d41d8cd98f00b204e9800998ecf8427e");
            string MD5s = null;
            Assert.That(DBFunc.MD5(MD5s) == "");
            byte[] MD5b = null;
            Assert.That(DBFunc.MD5(MD5b) == "");

            Assert.That(DBFunc.ParseInt("a") == 0);
            Assert.That(DBFunc.ParseInt("-1") == -1);
            Assert.That(DBFunc.ParseInt("1") == 1);
            var pt = DBFunc.ParseInt("a", int.MaxValue);
            Assert.That(pt == int.MaxValue, "maxval: " + pt);
            Assert.That(DBFunc.ParseInt("-1", 1) == -1);
            Assert.That(DBFunc.ParseInt("1", -1) == 1);

            Assert.That(DBFunc.Rand(1,1) == 1);
            Assert.That(DBFunc.Rand(-1, -1) == -1);
            Assert.That(DBFunc.Rand(1, 100) > 0);
            Assert.That(DBFunc.Rand(-1, -100) < 0);
            var randInt = DBFunc.Rand(-100, 100);
            Assert.That(randInt >= -100 && randInt <= 100);

            Assert.That(DBFunc.RandStr(0) == "");
            Assert.That(DBFunc.RandStr(1).Length == 1);
            Assert.That(DBFunc.RandStr(100).Length == 100);
            Assert.That(DBFunc.RandStr(1001).Length == 1001);
            Assert.That(DBFunc.RandStr(1, new []{'a'}) == "a");
            Assert.That(DBFunc.RandStr(10, new[] { 'x' }) == "xxxxxxxxxx");

            Assert.That(DBFunc.RandStrXtd(0) == "");
            Assert.That(DBFunc.RandStrXtd(10).Length == 10);

            Assert.That(DBFunc.UnixTimestamp() > 0);
            Assert.That(DBFunc.UnixTimestamp() < (DateTime.Now - new DateTime(1969,1,1)).TotalSeconds);

            Assert.That(DBFunc.ParseDateTime(null) == DateTime.MinValue);
            Assert.That(DBFunc.ParseDateTime("") == DateTime.MinValue);
            var dtNow = DateTime.Now;
            Assert.That(DBFunc.ParseDateTime("", dtNow) == dtNow);
            Assert.That(DBFunc.ParseDateTime("1/1/70 01:02:03") == new DateTime(1970, 1, 1, 1, 2, 3));
        }

        [Test]
        public void SqlHandling() {
            var shMain = DBHandler.GetHandler();
            Assert.That(!shMain.Connected);
            shMain.Connect();
            Assert.That(shMain.Connected);
            shMain.Connect();
            Assert.That(shMain.Connected);

            var sh = new SqlHandler(shMain.Host, shMain.Username, shMain.Password + "WRONG", shMain.Database);
            Assert.That(!sh.Connected);
            sh.Connect();
            Assert.That(!sh.Connected);
        }

        [Test]
        public void MediaFunctions() {
            Assert.That(!MediaFunc.ThumbFolderExists(-1));
            Assert.That(!MediaFunc.MovieFolderExists(-1, false));
            Assert.That(!MediaFunc.MovieFolderExists(-1, true));

            Assert.That(MediaFunc.GetThumbPath(-1) == "");
            Assert.That(MediaFunc.GetThumbPath(1) == "");
            Assert.That(MediaFunc.GetMoviePath(-1, false) == "");
            Assert.That(MediaFunc.GetMoviePath(1, false) == "");
            Assert.That(MediaFunc.GetMoviePath(-1, true) == "");
            Assert.That(MediaFunc.GetMoviePath(1, true) == "");

            MediaFunc.EmptyFoldersForMovie(-1);
            MediaFunc.DeleteFoldersForMovie(-1);
        }
    }
}